﻿#include "resmanager.h"

 

ResManager::ResManager(){

}

ResManager::~ResManager(){

}


int ResManager::parseAtFileContainer(SPtr<FileContainer> _fc){
	int res=0;

	switch(_fc.p()->getContainerType()){
		case CT_CONTAINER:
			break;

		case CT_USTRING:
		  UStrings.insert(createTAtParse<UString>(_fc));
			break;
		case CT_LANG_PAIR_USTRING:
		  LangPairStrings.insert(createTAtParse<LangPairString>(_fc));
			break;
		case CT_LANG_SELECTOR:
		  LangSelectors.insert(createTAtParse<LangSelector>(_fc));
			break;
		case CT_LANG_USTRING:
		  LangStrings.insert(createTAtParse<LangString>(_fc));
			break;
		case CT_LANGUAGES:
		  Languagess.insert(createTAtParse<Languages>(_fc));
			break;
		case CT_POSITION:
		  Positions.insert(createTAtParse<Position>(_fc));
			break;
		case CT_SIZE_XY:
		  SizeXYs.insert(createTAtParse<SizeXY>(_fc));
			break;
		case CT_LIMIT:
		  Limits.insert(createTAtParse<Limit>(_fc));
			break;
		case CT_TRANSFORMATION:
		  Transformations.insert(createTAtParse<Transformation>(_fc));
			break;
		/*MACROS006:
		case #:
		  #s.insert(createTAtParse<#>(_fc));
			break;
		*/

		case CT_ID_LIST_USTRING:
			IDListUStrings.insert(createListTAtParse<UString>(_fc));
			break;
		case CT_ID_LIST_LANG_PAIR_USTRING:
			IDListLangPairStrings.insert(createListTAtParse<LangPairString>(_fc));
			break;
		case CT_ID_LIST_LANG_SELECTOR:
			IDListLangSelectors.insert(createListTAtParse<LangSelector>(_fc));
			break;
		case CT_ID_LIST_LANG_USTRING:
			IDListLangStrings.insert(createListTAtParse<LangString>(_fc));
			break;
		case CT_ID_LIST_LANGUAGES:
		  IDListLanguagess.insert(createListTAtParse<Languages>(_fc));
			break;
		case CT_ID_LIST_POSITION:
		  IDListPositions.insert(createListTAtParse<Position>(_fc));
			break;
		case CT_ID_LIST_SIZE_XY:
		  IDListSizeXYs.insert(createListTAtParse<SizeXY>(_fc));
			break;
		case CT_ID_LIST_LIMIT:
		  IDListLimits.insert(createListTAtParse<Limit>(_fc));
			break;
		case CT_ID_LIST_TRANSFORMATION:
		  IDListTransformations.insert(createListTAtParse<Transformation>(_fc));
			break;
		/*MACROS007:
		case #:
		  IDList#s.insert(createListTAtParse<#>(_fc));
			break;
		*/

		case CT_INVALID:
			break;
	}

	vector<SPtr<FileContainer>>::iterator iter;
	for(iter=_fc.p()->getChilds()->begin();iter!=_fc.p()->getChilds()->end();iter++){
		if(parseAtFileContainer((*iter))<0)res=-1;
	}

	return res;
}

int ResManager::joinAll(){
	int res=0;
	long i=0;

	joinAllInMap<UString>(&UStrings);
	joinAllInMap<LangPairString>(&LangPairStrings);
	joinAllInMap<LangSelector>(&LangSelectors);
	joinAllInMap<LangString>(&LangStrings);
	joinAllInMap<Languages>(&Languagess);
  joinAllInMap<Position>(&Positions);
  joinAllInMap<SizeXY>(&SizeXYs);
	joinAllInMap<Limit>(&Limits);
	joinAllInMap<Transformation>(&Transformations);
	//MACROS008:joinAllInMap<#>(&#s);

	joinAllInMap<IDList<UString>>(&IDListUStrings);
	joinAllInMap<IDList<LangPairString>>(&IDListLangPairStrings);
	joinAllInMap<IDList<LangSelector>>(&IDListLangSelectors);
	joinAllInMap<IDList<LangString>>(&IDListLangStrings);
	joinAllInMap<IDList<Languages>>(&IDListLanguagess);
	joinAllInMap<IDList<Position>>(&IDListPositions);
	joinAllInMap<IDList<SizeXY>>(&IDListSizeXYs);
	joinAllInMap<IDList<Limit>>(&IDListLimits);
	joinAllInMap<IDList<Transformation>>(&IDListTransformations);
	//MACROS009:joinAllInMap<IDList<#>>(&IDList#s);

	return res;
}

int ResManager::checkAndSetAllObjectsAtIDs(){
	long res=0;

  if(checkAndSetObjectsAtIDsInMap<UString>(&UStrings)<0)res=-1;
  if(checkAndSetObjectsAtIDsInMap<LangPairString>(&LangPairStrings)<0)res=-2;
  if(checkAndSetObjectsAtIDsInMap<LangSelector>(&LangSelectors)<0)res=-3;
  if(checkAndSetObjectsAtIDsInMap<LangString>(&LangStrings)<0)res=-4;
  if(checkAndSetObjectsAtIDsInMap<Languages>(&Languagess)<0)res=-4;
	if(checkAndSetObjectsAtIDsInMap<Position>(&Positions)<0)res=-4;
	if(checkAndSetObjectsAtIDsInMap<SizeXY>(&SizeXYs)<0)res=-4;
	if(checkAndSetObjectsAtIDsInMap<Limit>(&Limits)<0)res=-4;
	if(checkAndSetObjectsAtIDsInMap<Transformation>(&Transformations)<0)res=-4;
	//MACROS010:if(checkAndSetObjectsAtIDsInMap<#>(&#s)<0)res=-4;
  
	if(checkAndSetObjectsAtIDsInMap<IDList<UString>>(&IDListUStrings)<0)res=-5;
  if(checkAndSetObjectsAtIDsInMap<IDList<LangPairString>>(&IDListLangPairStrings)<0)res=-6;
  if(checkAndSetObjectsAtIDsInMap<IDList<LangSelector>>(&IDListLangSelectors)<0)res=-7;
  if(checkAndSetObjectsAtIDsInMap<IDList<LangString>>(&IDListLangStrings)<0)res=-8;
  if(checkAndSetObjectsAtIDsInMap<IDList<Languages>>(&IDListLanguagess)<0)res=-8;
	if(checkAndSetObjectsAtIDsInMap<IDList<Position>>(&IDListPositions)<0)res=-8;
	if(checkAndSetObjectsAtIDsInMap<IDList<SizeXY>>(&IDListSizeXYs)<0)res=-8;
	if(checkAndSetObjectsAtIDsInMap<IDList<Limit>>(&IDListLimits)<0)res=-8;
	if(checkAndSetObjectsAtIDsInMap<IDList<Transformation>>(&IDListTransformations)<0)res=-8;
	//MACROS011:if(checkAndSetObjectsAtIDsInMap<IDList<#>>(&IDList#s)<0)res=-8;
	
	return res;
}


int ResManager::addUString(SPtr<UString> val, SPtr<FileContainer> *_ParrentFC){
	return addT<UString>(&UStrings, val ,_ParrentFC);
}
int ResManager::addLangPairString(SPtr<LangPairString> val, SPtr<FileContainer> *_ParrentFC){
	return addT<LangPairString>(&LangPairStrings, val ,_ParrentFC);
}
int ResManager::addLangSelector(SPtr<LangSelector> val, SPtr<FileContainer> *_ParrentFC){
	return addT<LangSelector>(&LangSelectors, val ,_ParrentFC);
}
int ResManager::addLangString(SPtr<LangString> val, SPtr<FileContainer> *_ParrentFC){
	return addT<LangString>(&LangStrings, val ,_ParrentFC);
}
int ResManager::addLanguages(SPtr<Languages> val, SPtr<FileContainer> *_ParrentFC){
	return addT<Languages>(&Languagess, val ,_ParrentFC);
}
int ResManager::addPosition(SPtr<Position> val, SPtr<FileContainer> *_ParrentFC){
	return addT<Position>(&Positions, val ,_ParrentFC);
}
int ResManager::addSizeXY(SPtr<SizeXY> val, SPtr<FileContainer> *_ParrentFC){
	return addT<SizeXY>(&SizeXYs, val ,_ParrentFC);
}
int ResManager::addLimit(SPtr<Limit> val, SPtr<FileContainer> *_ParrentFC){
	return addT<Limit>(&Limits, val ,_ParrentFC);
}
int ResManager::addTransformation(SPtr<Transformation> val, SPtr<FileContainer> *_ParrentFC){
	return addT<Transformation>(&Transformations, val ,_ParrentFC);
}
/*MACROS012:
int ResManager::add#(SPtr<#> val, SPtr<FileContainer> *_ParrentFC){
	return addT<#>(&#s, val ,_ParrentFC);
}
*/

SPtr<UString> *ResManager::getUStringAtID(__int64 _id){
	return getTAtID<UString>(&UStrings,_id);
}
SPtr<LangPairString> *ResManager::getLangPairStringAtID(__int64 _id){
	return getTAtID<LangPairString>(&LangPairStrings,_id);
}
SPtr<LangSelector> *ResManager::getLangSelectorAtID(__int64 _id){
	return getTAtID<LangSelector>(&LangSelectors,_id);
}
SPtr<LangString> *ResManager::getLangStringAtID(__int64 _id){
	return getTAtID<LangString>(&LangStrings,_id);
}
SPtr<Languages> *ResManager::getLanguagesAtID(__int64 _id){
	return getTAtID<Languages>(&Languagess,_id);
}
SPtr<Position> *ResManager::getPositionAtID(__int64 _id){
	return getTAtID<Position>(&Positions,_id);
}
SPtr<SizeXY> *ResManager::getSizeXYAtID(__int64 _id){
	return getTAtID<SizeXY>(&SizeXYs,_id);
}
SPtr<Limit> *ResManager::getLimitAtID(__int64 _id){
	return getTAtID<Limit>(&Limits,_id);
}
SPtr<Transformation> *ResManager::getTransformationAtID(__int64 _id){
	return getTAtID<Transformation>(&Transformations,_id);
}
/*MACROS013:
SPtr<#> *ResManager::get#AtID(__int64 _id){
	return getTAtID<#>(&#s,_id);
}
*/


int ResManager::getObjsToIDList_UString(map<__int64, SPtr<UString>> *mapIDs){
	return getObjsToIDList_T<UString>(&ResManager::getUStringAtID,mapIDs);
}
int ResManager::getObjsToIDList_LangPairString(map<__int64, SPtr<LangPairString>> *mapIDs){
	return getObjsToIDList_T<LangPairString>(&ResManager::getLangPairStringAtID,mapIDs);
}
int ResManager::getObjsToIDList_LangSelector(map<__int64, SPtr<LangSelector>> *mapIDs){
  return getObjsToIDList_T<LangSelector>(&ResManager::getLangSelectorAtID,mapIDs);
}
int ResManager::getObjsToIDList_LangString(map<__int64, SPtr<LangString>> *mapIDs){
	return getObjsToIDList_T<LangString>(&ResManager::getLangStringAtID,mapIDs);
}
int ResManager::getObjsToIDList_Languages(map<__int64, SPtr<Languages>> *mapIDs){
	return getObjsToIDList_T<Languages>(&ResManager::getLanguagesAtID,mapIDs);
}
int ResManager::getObjsToIDList_Position(map<__int64, SPtr<Position>> *mapIDs){
	return getObjsToIDList_T<Position>(&ResManager::getPositionAtID,mapIDs);
}
int ResManager::getObjsToIDList_SizeXY(map<__int64, SPtr<SizeXY>> *mapIDs){
	return getObjsToIDList_T<SizeXY>(&ResManager::getSizeXYAtID,mapIDs);
}
int ResManager::getObjsToIDList_Limit(map<__int64, SPtr<Limit>> *mapIDs){
	return getObjsToIDList_T<Limit>(&ResManager::getLimitAtID,mapIDs);
}
int ResManager::getObjsToIDList_Transformation(map<__int64, SPtr<Transformation>> *mapIDs){
	return getObjsToIDList_T<Transformation>(&ResManager::getTransformationAtID,mapIDs);
}
/*MACROS014:
int ResManager::getObjsToIDList_#(map<__int64, SPtr<#>> *mapIDs){
	return getObjsToIDList_T<#>(&ResManager::get#AtID,mapIDs);
}
*/

int ResManager::addAndParseAtFile(wstring &fileName){
	SPtr<FileContainer> fc;
	fc.p()->setFileName(fileName);
	fc.p()->load();
	parseAtFileContainer(fc);
  return 0;
}

int ResManager::addAndParseAtContainer(SPtr<FileContainer> _inFC, SPtr<FileContainer> *_ParrentFC){
	if(_ParrentFC==NULL){
		fileContainers.insert(pair<__int64,SPtr<FileContainer>>(_inFC.p()->getID(),_inFC));
		parseAtFileContainer(_inFC);
		return 0;
	}
	_ParrentFC->p()->addFileContainerToChilds(_inFC);
	parseAtFileContainer(_inFC);
	return 0;
}

int ResManager::saveAndJoinAll(){
	int res=0;
	map<__int64,SPtr<FileContainer>>::iterator iter;
	int i=0;
	joinAll();
	for(iter=fileContainers.begin();iter!=fileContainers.end();iter++){
		i--;
		if((*iter).second.p()->save()<0){
			if(res==0)res=i;
		}
	}
	return res;
}